<?php

namespace Common\Model;
use think\Model;

class StaffModel extends BaseModel{

	//追加属性
    public $append = [
        //'status_text',   //状态
        'gx_list',      //工序列表
        'sex_text',     //性别
        'stGid_text',   //小组
        'glzhc',//关联账号
        'username',//会员账号
        'jinbanr',//经办人
        'chejian',
        'stIszc_text',
        'stIsjb_text'
	];
 
     //性别
     function getJinbanrAttr($val, $data){
        $text = '否';
        if($data['stIsjb']==1){
            $text = '是';
        }
        return $text;
    }
    function getUserNameAttr($val, $data){
        if($data['uid']){
             $model = M('user');
             $text=$model->where(['uid'=>$data['uid']])->getField('username');
        }
        return $text;
    }
    //小组
    function getStGidTextAttr($val, $data){
        if($data['stCid'] >0){
             $model = M('jicgroups');
             $text=$model->where(['gId'=>$data['stCid']])->getField('gName');
        }elseif($data['stGid'] >0){
             $model = M('groups');
             $text=$model->where(['gId'=>$data['stGid']])->getField('gName');
        }else{
             $text = '<span style="color:red">未分配小组</span>';
        } 
        if(MODULE_NAME=='Api' || MODULE_NAME=='YgApi'){
            $text = strip_tags($text);
        }
        return $text;
    }
    //性别
    function getSexTextAttr($val, $data){
        $text = '未知';
        if($data['stSex']==1){
            $text = '男';
        }else{
            $text = '女'; 
        }
        return $text;
    }
    //工序列表
    function getGxListAttr($val, $data){
        if($data['gx_cc']){
            $model = M('Product');
            $wdata['pId'] = array('in',$data['gx_cc']);
            $list = $model->field('pId,pName,pPrice')->where($wdata)->select();
        }else{
            $list = [];    
        }
        return $list;
    }
    function getstIszcTextAttr($val, $data){
        $text = '--';
        if($data['stIszc']==1){
            $text = '组长';
        }else{
            $text = '组员'; 
        }
        return $text;
    }
    function getstIsjbTextAttr($val, $data){
        $text = '--';
        if($data['stIsjb']==1){
            $text = '在职';
        }else{
            $text = '离职'; 
        }
        return $text;
    }

    //搜索
    function _before_search(){
        $map = [];
        if(I('keyword')){
            $map['stName'] = array('like','%'.I('keyword').'%');
        }
        if(I('stCid')){
            $map['stCid'] =I('stCid');
        }
        if(I('stype')){
            $map['stGid'] =I('stype');
        }
        $map['deletetime'] = 0;
        return $map;
    }

}